/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.qkorder.ordermanager;

import cn.ekuma.epos.bean.util.erp.OrderLineHelper;
import com.openbravo.format.Formats;
import com.openbravo.pos.util.RoundUtils;
import com.openbravo.bean.erp.OrderLine;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class OrderPageEntry implements Comparable{
    private int pageIndex;
    private List<OrderLine> m_aLines;

    public OrderPageEntry(int pageIndex) {
        this.pageIndex = pageIndex;
        m_aLines=new ArrayList();
    }

    public void setOrderLines(List<OrderLine> m_aLines){
    	this.m_aLines=m_aLines;
    }
    
    public int getPageIndex() {
        return pageIndex;
    }
    
    public OrderLine getLine(int index) {
        return m_aLines.get(index);
    }
  
    List<OrderLine> getLines() {
        return m_aLines;
    }

    public void addLine(OrderLine oLine) {
        oLine.setPageIndex(pageIndex);
        m_aLines.add(oLine);
    }

    public void insertLine(int index, OrderLine oLine) {
        oLine.setPageIndex(pageIndex);
        m_aLines.add(index, oLine);
    }

    public void setLine(int index, OrderLine oLine) {
        oLine.setPageIndex(pageIndex);
        m_aLines.set(index, oLine);
    }

    public void removeLine(int index) {
        m_aLines.remove(index);
    }

    public int getLinesCount() {
        return m_aLines.size();
    }

    public double getSubTotal() {
        double sum = 0.0;
        for (OrderLine line : m_aLines) {
            sum = RoundUtils.add(sum,OrderLineHelper.getSubValue(line));
        }
        return sum;
    }

    public double getTax() {
       double sum = 0.0;
       for (OrderLine line : m_aLines) {
                sum =RoundUtils.add(sum, OrderLineHelper.getTax(line));
       }
        return sum;
    }

    public double getTotal() {
        return RoundUtils.add(getSubTotal() , getTax());
    }

    public double getArticlesCount() {
        double dArticles = 0.0;
        OrderLine oLine;

        for (Iterator<OrderLine> i = m_aLines.iterator(); i.hasNext();) {
            oLine = i.next();
            dArticles =RoundUtils.add(dArticles, oLine.getMultiply());
        }

        return dArticles;
    }

     public double getDefaultSubTotal(){
        double sum = 0.0;
        for (OrderLine line : getLines()) {
            sum =RoundUtils.add(sum,OrderLineHelper.getDefaultSubValue(line));
        }
        return sum;
    }

    public double getDefaultTax(){
        double sum = 0.0;
        for (OrderLine line : getLines()) {
            sum =RoundUtils.add(sum, OrderLineHelper.getDefaultTax(line));
        }
        return sum;
    }

    public double getDefaultTotal(){
        return RoundUtils.add(getDefaultSubTotal(),getDefaultTax());
    }


    @Override
    public int compareTo(Object o) {
        OrderPageEntry obj=(OrderPageEntry)o;
        return pageIndex-obj.getPageIndex() ;
    }

    @Override
    public boolean equals(Object o) {
        OrderPageEntry obj=(OrderPageEntry)o;
        return obj.getPageIndex()==pageIndex;
    }

    public String printArticlesCount() {
        return Formats.DOUBLE.formatValue(new Double(getArticlesCount()));
    }

    public String printSubTotal() {
        return Formats.CURRENCY.formatValue(new Double(getSubTotal()));
    }

    public String printTax() {
        return Formats.CURRENCY.formatValue(new Double(getTax()));
    }

    public String printTotal() {
        return Formats.CURRENCY.formatValue(new Double(getTotal()));
    }

    public String printDefaultSubTotal() {
        return Formats.CURRENCY.formatValue(new Double(getDefaultSubTotal()));
    }

    public String printDefaultTax() {
        return Formats.CURRENCY.formatValue(new Double(getDefaultTax()));
    }

    public String printDefaultTotal() {
        return Formats.CURRENCY.formatValue(new Double(getDefaultTotal()));
    }
}
